package com.dadibadi.core.work.service;

import com.dadibadi.core.dao.entity.WorkInfoTab;
import com.dadibadi.core.dao.mapper.WorkInfoMapper;
import com.dadibadi.core.work.qo.WorkInfoIdQO;
import com.dadibadi.core.work.qo.WorkInfoListQO;
import com.dadibadi.core.work.vo.WorkInfoDetailVO;
import com.dadibadi.core.work.vo.WorkInfoListVO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.venusoft.developer.common.utils.ConvertUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
@Slf4j
public class WorkInfoService {
    @Resource
    WorkInfoMapper workInfoMapper;

    public WorkInfoDetailVO getWorkInfo(WorkInfoIdQO qo) {
        WorkInfoTab workInfoTab = workInfoMapper.selectById(qo.getWorkId());
        WorkInfoDetailVO vo = ConvertUtil.convert(workInfoTab, WorkInfoDetailVO.class);
        return vo;

    }

    public List<WorkInfoListVO> getWorkInfoByUser(WorkInfoIdQO qo) {
        MPJLambdaWrapper<WorkInfoTab> wrapper = new MPJLambdaWrapper<WorkInfoTab>()
                .eq(WorkInfoTab::getUserId, qo.getUserId());
        List<WorkInfoTab> list = workInfoMapper.selectList(wrapper);
        Collections.sort(list, Comparator.comparing(WorkInfoTab::getCreatedAt, (t1, t2) -> t2.compareTo(t1)));


        List<WorkInfoListVO> result = ConvertUtil.convert(list, WorkInfoListVO.class);
        return result;
    }

    public List<WorkInfoListVO> searchWorkInfo(WorkInfoListQO qo) {
        MPJLambdaWrapper<WorkInfoTab> wrapper = new MPJLambdaWrapper<WorkInfoTab>()
                .like(WorkInfoTab::getTitle, qo.getTitle());

        List<WorkInfoTab> list = workInfoMapper.selectList(wrapper);
        Collections.sort(list, Comparator.comparing(WorkInfoTab::getCreatedAt, (t1, t2) -> t2.compareTo(t1)));
        List<WorkInfoListVO> result = ConvertUtil.convert(list, WorkInfoListVO.class);
        return result;

    }
    public int getUserArticleCount(List<WorkInfoListVO> list){
        if(list==null) return 0;
        return list.size();
    }
}
